Skip to content

fix(libkernel): clear reported local address after UDP disconnect - #12467

Merged
adriadam10 merged 5 commits into
boykopovar:mainfrom
Buraddomoon:fix/udp-disconnect-local-address
Oct 30, 2026
Merged

adriadam10 merged 5 commits into
boykopovar:mainfrom
Buraddomoon:fix/udp-disconnect-local-address

Conversation

@Buraddomoon

@Buraddomoon Buraddomoon commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

What

Correct the local address reported by getsockname after a UDP association is dissolved, preserving the assigned port. The logical wildcard state is shared by duplicated descriptors and cleared on successful bind or reconnect. Native UDP address operations and their state updates use the existing socket mutex, so overlapping disconnects and reconnects cannot leave stale state. Stream calls retain main's locking. IPv6 wildcard reporting clears the obsolete scope ID.

Depends on #10544 (5cc1ea83ca93b66816eb1ce4211bd5cbe49d21db), included as an ancestor. Its macOS post-error disconnect verification is shared by POSIX hosts so the same production path can be tested on Linux. A native disconnect error is tolerated only when getpeername confirms ENOTCONN. A still-connected socket or an indeterminate peer query preserves the original native error. Successful disconnection then follows the existing guest error mapping: 47 for a correctly sized AF_UNSPEC address, 49 for a port-zero destination. The Windows native disconnect branch is unchanged.

The initial reported-name implementation is adapted from the branch linked in issue #8681, with the original author credited as co-author. Behavior references: FreeBSD 11.4 udp_disconnect, udp6_disconnect and in6_getsockaddr / in6_sockaddr. No new console measurement is claimed.

Regression coverage includes IPv4/IPv6, implicit/port-zero/explicit-port bindings, descriptor aliases, reconnection, datagram delivery and truncated outputs with trailing sentinels. Linux integration fixtures cover overlapping native disconnect/reconnect operations and 72 host-result cases: injected EAFNOSUPPORT/EINVAL after actual disconnection, genuine failure while still connected, and a failed post-error peer query, for both guest disconnect entry points and all binding modes. The latter fixture models the macOS return values while calling the shared production POSIX implementation.

The debt entry retains the native-binding limitation: a host socket may keep its explicit local address, and the documented Windows sendto failure to non-loopback destinations remains.

Tested

Debian 13 container on Fedora 44 x86-64, GCC 14.2.0, CMake 3.31.6, Ninja. Head 9a4d2462d3e5fe2fc38a49348bd1a45cf55458f3, based on main b9eafb0c495d273e0bcf80aacc51350db6861ede; prerequisite #10544 is included as a merge parent.

  • Configured the full runtime with cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON; built libc, libkernel and the selected guest test executables.
  • 14/14 CTest cases passed: guest_udp, guest_udp_name_concurrency, guest_udp_disconnect_result, guest_socket_port_zero, guest_dup, guest_socket_pair, guest_recvmsg_memory, guest_socket_descriptor_flags, guest_socket_address_length, guest_socket_options, guest_socket_options_multicast, guest_socket_read_write, guest_socket_descriptors, guest_socket_connect_listening.
  • Negative control: separately compiled reviewed-head a73515ead1's Udp.cpp against the same runtime objects and linked it into an isolated library. The new guest_udp_disconnect_result regression aborts (CTest exit 8). The corrected implementation passes. Earlier negative controls also established the sequential reported-name and concurrent-transition regressions.
  • AddressSanitizer + UBSan: 3/3 tests passed, with no sanitizer diagnostics: guest_udp, guest_udp_name_concurrency, guest_udp_disconnect_result. A separate RelWithDebInfo build uses -DANYPS5_ENABLE_ASAN=ON and -DCMAKE_CXX_FLAGS="-fsanitize=undefined -fno-sanitize=vptr -fno-sanitize-recover=undefined", with ASAN_OPTIONS=detect_leaks=0 and UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1.
  • Conventions with the complete PR body, public-header declaration audit, CTest/script registration audits and git diff --check passed.
  • python3 tools/pr_overlap.py --branch fix/udp-disconnect-local-address --jobs 4 fetched 1,355 open heads and compared the other 1,353 after excluding this head and its inherited prerequisite fix(libkernel): count a dissolved datagram association as disconnected on macOS #10544. No conflicts were reported among comparable heads. The tool could not compare 34 heads already conflicting with main; manual review of their shared-file changes found no implementation of this behavior. fix(libkernel): count a dissolved datagram association as disconnected on macOS #10544 remains an explicit dependency.

macOS hardware was not available. The macOS error-result mapping is covered by the Linux fault-injection fixture above; this is not a native macOS build or run. Windows, console hardware and games were not run. Only the selected runtime libraries and tests were built; no all-libraries build or hosted runtime-CI pass is claimed. The pre-existing unusable-pointer gap in getsockname remains separate work in PR #11281.

Checklist

…d on macOS

On macOS, connect to AF_UNSPEC on a connected datagram socket dissolves
the association but returns -1, with EAFNOSUPPORT for IPv4 and EINVAL
for IPv6. Disconnect took that as a failure, so connect to port 0 on a
connected datagram socket failed with 47 instead of EADDRNOTAVAIL (49).
Treat the call as successful when the socket is no longer connected.

Co-authored-by: marion909 <103169397+marion909@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@rooklz rooklz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

macOS evidence for the disconnect path (the body notes macOS was not run). This is a non-blocking note: the gap predates this PR, but it means the new CheckDissolvedName cases cannot pass on a macOS libs build.

Host primitive, macOS 26.2 arm64, plain C probe. Each case is a connected UDP socket followed by connect({sa_family = AF_UNSPEC}, sizeof(sockaddr)), which is the exact call Disconnect makes:

family binding connect(AF_UNSPEC) getpeername after getsockname after
inet implicit / 127.0.0.1:0 / 127.0.0.1:<port> -1, errno 47 -1/57 (dissolved) 0.0.0.0:<port>
inet6 implicit / [::1]:0 / [::1]:<port> -1, errno 22 (len 16; len 28 gives 47) -1/57 (dissolved) [::]:<port>

XNU therefore dissolves the association and also resets the local address to the wildcard while keeping the port, as FreeBSD's udp_disconnect does. It still reports failure. A later bind to the wildcard address fails with EINVAL on macOS and succeeds on Linux.

Disconnect / ZeroPortConnect copied verbatim from fb97e38f16 (and from main 17f8dfad51), with real host sockets and only NativeListening stubbed:

host path guest rc / errno localAddressCleared
Linux (ubuntu 24.04 arm64 container, g++) PR + main AF_UNSPEC 0 PR: 1
Linux port 0 -1 / 49 PR: 1
macOS (clang++) PR + main inet AF_UNSPEC -1 / 47 (correct only by coincidence: it is the host errno) 0
macOS inet port 0 -1 / 47 (expected 49) 0
macOS inet6 AF_UNSPEC -1 / 22 (expected 47) 0
macOS inet6 port 0 -1 / 22 (expected 49) 0

On macOS the early return Fail(NativeError()) fires every time. The new flag is never set, so getsockname keeps whatever the host reports, and the port-0 disconnect returns the host errno instead of 49. Because XNU already reports the wildcard address there, only the return values are wrong on macOS today. Even so, CheckDissolvedName (connect(...) == -1 && errno == (portZero ? 49 : 47)) would fail on its inet port-0 iteration and on both inet6 iterations. The existing inet connect_nid_postfix(dissolved, unspecified) == -1 && errno == 47 passes only because the host errno happens to be 47. The macOS CI lanes do not build guest_udp, which is why nobody has seen this.

A one-line tolerance that gave the expected row on both hosts in the harness (macOS: all 4 rows 0 / 49 with cleared=1; Linux: unchanged):

-    if (::connect(socket.value, &unspecified, sizeof(unspecified)) != 0) return Fail(NativeError());
+    if (::connect(socket.value, &unspecified, sizeof(unspecified)) != 0 && PeerConnected(socket)) return Fail(NativeError());

PeerConnected already exists. The wildcard re-bind that follows only runs when the port reads 0, and on macOS the port is kept, so the EINVAL re-bind is never reached. Whether this belongs in this PR or a follow-up is your call. If it goes in a follow-up, a line in the AF_UNSPEC debt entry about the macOS return value would keep the record accurate.

I did not run the full libs build on macOS, only the copied functions, so this is a COMMENT and not a review verdict. The PR's own logic (clear on a successful connect/bind, shared through dup via the shared Socket) reads correctly against FreeBSD 11.4's udp_disconnect/in_pcbconnect.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On macOS, the native connect(AF_UNSPEC) can dissolve a UDP association and reset the local address while still returning an error. The macOS 26.2 arm64 probes in the existing review comment reproduce this for both IPv4 and IPv6; the copied Disconnect path returns before setting localAddressCleared.

This also makes the new CheckDissolvedName cases fail on macOS: the port-zero case expects guest error 49, while XNU returns host errno 47 (IPv4) or 22 (IPv6); the IPv6 AF_UNSPEC case likewise receives 22 instead of guest error 47. The existing IPv4 AF_UNSPEC assertion passes only because the host errno happens to equal 47. These tests are currently omitted from the macOS CI lane, so the green hosted checks do not cover this.

Please account for the host's “disconnected but returned error” result (for example, distinguish it from a still-connected socket before returning the native error), preserve the guest error mapping, and run the UDP regression on macOS or add a focused platform-independent test for the mapping. If this is intentionally deferred, the PR should avoid asserting cross-platform behavior it does not provide and document the macOS gap in the debt entry.

@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 12688 core/libs/prx/libkernel/Socket/src/Udp.cpp L1348-1352
PR 12700 core/libs/prx/libkernel/Socket/src/Udp.cpp L1348-1352
PR 12965 core/libs/prx/libkernel/Socket/src/Udp.cpp L1348-1352

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 4563 core/libs/prx/libkernel/CMakeLists.txt
PR 4630 docs/dev/debt/libkernel.md
PR 5021 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 6936 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 7088 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 7105 docs/dev/debt/libkernel.md
PR 7121 docs/dev/debt/libkernel.md
PR 7135 docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7192 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 7447 docs/dev/debt/libkernel.md
PR 7464 docs/dev/debt/libkernel.md
PR 7509 docs/dev/debt/libkernel.md
PR 7804 docs/dev/debt/libkernel.md
PR 8477 core/libs/prx/libkernel/CMakeLists.txt
PR 8605 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 9156 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9242 docs/dev/debt/libkernel.md
PR 9293 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9297 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9302 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9326 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9383 core/libs/prx/libkernel/CMakeLists.txt
PR 9389 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9557 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9566 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9843 docs/dev/debt/libkernel.md
PR 9938 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 10442 docs/dev/debt/libkernel.md
PR 10552 docs/dev/debt/libkernel.md
PR 10645 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 11002 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 11215 docs/dev/debt/libkernel.md
PR 11628 docs/dev/debt/libkernel.md
PR 11661 docs/dev/debt/libkernel.md
PR 11689 docs/dev/debt/libkernel.md
PR 11698 docs/dev/debt/libkernel.md
PR 11827 docs/dev/debt/libkernel.md
PR 11939 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 12135 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 12544 core/libs/prx/libkernel/CMakeLists.txt
PR 12560 docs/dev/debt/libkernel.md
PR 12741 docs/dev/debt/libkernel.md
PR 12826 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 12854 docs/dev/debt/libkernel.md
PR 12908 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 12970 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md

@github-actions github-actions Bot added the fix error Closes an issue (Fixes #N) label Oct 28, 2026
Buraddomoon and others added 4 commits October 28, 2026 13:36
Preserve the local port while reporting a wildcard address after a datagram association is dissolved. Share the state across descriptor aliases and clear it on a successful bind or reconnect.

Adapted from the fix branch linked in issue boykopovar#8681, with expanded binding, reconnect and descriptor-alias regression coverage.

Co-authored-by: xXPBSXx <20614131+xXPBSXx@users.noreply.github.com>
Keep native UDP bind, connect and disconnect operations coherent with the shared reported-address state. Reuse the existing connect lock and avoid additional locking on stream sockets.

Add a controlled overlapping disconnect/reconnect regression and check truncated address writes after disconnection. Clear an obsolete IPv6 scope ID when reporting the wildcard address.
Reuse the macOS disconnect prerequisite in the common POSIX path. Proceed only when getpeername positively reports ENOTCONN, retaining the original connect error for connected or indeterminate sockets.

Exercise actual UDP sockets with injected EAFNOSUPPORT and EINVAL results for IPv4 and IPv6, both disconnect entry points, all three binding modes, and still-connected or failed peer-query outcomes.
@Buraddomoon
Buraddomoon force-pushed the fix/udp-disconnect-local-address branch from a73515e to 9a4d246 Compare October 28, 2026 16:39
@Buraddomoon

Copy link
Copy Markdown
Contributor Author

@MarcZX10 @rooklz The disconnect-result feedback is addressed in the current head, 9a4d2462d3e5fe2fc38a49348bd1a45cf55458f3.

The PR now inherits #10544 as an explicit dependency. Its post-error verification is shared by the POSIX path: processing continues only when getpeername confirms ENOTCONN. A still-connected socket or an indeterminate peer query preserves the original connect error. This keeps guest errors 47 for a correctly sized AF_UNSPEC address and 49 for a port-zero destination after successful disconnection.

Added guest_udp_disconnect_result, a real-socket Linux integration fixture covering 72 combinations of IPv4/IPv6, binding modes, both guest disconnect entry points, injected EAFNOSUPPORT/EINVAL after actual disconnection, still-connected failures, and failed peer queries. It fails against the reviewed head's Udp.cpp (a73515ead1) and passes with the correction.

Validation on the updated branch: 14/14 focused CTest cases passed, plus 3/3 with AddressSanitizer and UBSan, with no sanitizer diagnostics. Conventions and test-registration audits passed. Native macOS hardware was unavailable; the fixture simulates the reported macOS error results on Linux and exercises the shared production implementation. The PR description records this limitation and the dependency.

Could you please re-review the current head? Thank you for the probes and the review.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-checked — current head approved

The revised disconnect path handles POSIX hosts that dissolve a UDP association but return an error: it proceeds only when a follow-up getpeername proves the socket is disconnected, and otherwise preserves the original error. The new Linux regression exercises disconnected, still-connected, and indeterminate peer-query outcomes across the IPv4/IPv6 and binding cases. The synchronization changes also protect the shared address state during disconnect, bind, reconnect, and name lookup.

What I ran

  • Reviewed current head 9a4d2462d3e5fe2fc38a49348bd1a45cf55458f3 against main 319621ba2e578bd053b0baba2877fe70520e0737.
  • Inspected the complete diff, including the common POSIX error-handling path and the new Linux-only integration test.
  • Ran git diff --check refs/review/base...refs/review/pr-12467.
  • Generated a merge preview with git merge-tree --write-tree refs/review/base refs/review/pr-12467.
  • Refreshed hosted checks.

Results

Check Result
git diff --check pass
Merge preview against current main clean; tree 9e7ff9c0a4312277b793e0f6848d360ffdb5e286
Hosted conventions, labels, eligibility, report pass
Hosted Windows workflow pass
Hosted Linux/product and specialized jobs skipped
Focused Linux CTest and ASan/UBSan runs author reports 14/14 CTest cases and 3/3 sanitizer tests passing; I did not independently rerun them

Findings

Blocking

None found in the current diff.

Non-blocking

The Linux integration test exercises the common POSIX branch, but it does not replace a native macOS build/test run. The PR records that macOS was unavailable; the error-result behavior has been independently probed on macOS in an existing review comment.

Sources

Not verified

I did not run a local C++ build, CTest, sanitizer run, native macOS or Windows test, or console test on this head. The hosted Windows workflow passed; hosted Linux/product jobs were skipped.

To get this to a good state

No blocking change requested. The exact-head diff, merge preview, hosted checks, and reported focused tests support approval.

@MarcZX10

Copy link
Copy Markdown
Contributor

Correction to the macOS probe source link in my review: the intended source is rooklz’s existing review. The review ID in my previous link was incorrect.

@adriadam10
adriadam10 merged commit bcee950 into boykopovar:main Oct 30, 2026
12 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the disconnect and reported-name paths against the diff and FreeBSD 11.4 udp_disconnect, with #10544 now on main; merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build docs fix error Closes an issue (Fixes #N) libkernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

libkernel: getsockname still returns the old local address after a UDP association is dissolved

5 participants